<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Type extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();

        $this->load->helper(array('form', 'url'));
        $this->load->library(array('session'));
        $this->load->database();

        //加载模块
        $this->load->model('type_model');

        //调试模式
        //$this->output->enable_profiler(TRUE);
    }

    /****************** 页面显示 ***********************
     * index => 分类表
     * add => 添加分类
     * edit => 编辑分类
     */

    public function index()
    {
        if(islogin() == false){jump('/admin/login','请先登录');return;}

        //定义网页相关数据
        $data['title'] = 'Li Lijie - 分类管理';
        $data['list'] = $this->type_model->getlist();

        //载入
        $this->load->view('admin/header',$data);
        $this->load->view('admin/typelist');
        $this->load->view('admin/footer');
    }

    public function add()
    {
        if(islogin() == false){jump('/admin/login','请先登录');return;}

        //定义网页相关数据
        $data['title'] = 'Li Lijie - 添加分类';
        //载入
        $this->load->view('admin/header',$data);
        $this->load->view('admin/typeadd');
        $this->load->view('admin/footer');
    }

    public function edit($id)
    {
        if(islogin() == false){jump('/admin/login','请先登录');return;}

        //定义网页相关数据
        $data['title'] = 'Li Lijie - 编辑分类';
        $data['type'] = $this->type_model->get($id);

        //载入
        $this->load->view('admin/header',$data);
        $this->load->view('admin/typeedit');
        $this->load->view('admin/footer');
    }

    /****************** 相关操作 ***********************
     * addtype => 添加
     * edittype => 编辑
     * deltype => 删除
     */

    public function addtype()
    {
        if(islogin() == false){jump('/admin/login','请先登录');return;}

        //获取表单信息
        $data = array(
            'name' => $this->input->post('name')
        );
        //数据库操作
        $this->type_model->add($data);
        jump('/admin/type','添加成功！');
    }

    public function edittype()
    {
        if(islogin() == false){jump('/admin/login','请先登录');return;}

        $id = $this->input->post('id');  //id
        $data = array(
            'name' => $this->input->post('name')  //标题
        );
        //数据库操作
        $this->type_model->update($id,$data);
        jump('/admin/type','修改成功！');
    }

    public function  deltype($id)
    {
        if(islogin() == false){jump('/admin/login','请先登录');return;}

        //数据库操作
        $this->type_model->del($id);
        jump('/admin/type','删除成功！');
    }
}